//
//  SIStatusListDAL.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/14.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

/// DAL 数据访问层
///使命: 给ListViewModel提供微博从数据库加载的字典数组
class SIStatusListDAL {

    /// 从本地数据库或网络加载数据
    class func loadStatus(since_id: Int64 = 0, max_id: Int64 = 0, completion:(list: [[String: AnyObject]]?, isSuccess: Bool) -> ()) {
        
        guard let userId = SINetworkTools.shared.userAccount.uid else {
            
            return
            
        }
        
        let array = SISQLiteManager.shared.loadStatus(userId: userId, since_id: since_id, max_id: max_id)
        
        if array.count > 0 {
            
            completion(list: array, isSuccess: true)
            
            return
        }
        
        SINetworkTools.shared.statusList(since_id: since_id, max_id: max_id) { (list, isSuccess) in
            
            if !isSuccess {
                
                completion(list: nil, isSuccess: false)
                
                return
                
            }
            
            guard let list = list else {
                
                completion(list: nil, isSuccess: isSuccess)
                return
            }
            
            SISQLiteManager.shared.updateStatus(userId: userId, array: list)
            
            completion(list: list, isSuccess: isSuccess)
            
        }
        
        
    }
    
}
